package com.validation.factory;

import com.validation.builder.EncryptionConfig;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Administrator
 */
@Component
@DependsOn(value = "encryptionConfig")
public class InitEncryptionFactory {
    private static final int SHUFFLE_THRESHOLD = 5;
    public int Q = 0;
    public int R = 0;
    @Resource
    private EncryptionConfig encryptionConfig;
    public ConcurrentHashMap<String, String> CD_MAP = null;
    public ConcurrentHashMap<String, String> OD_MAP = null;

    @PostConstruct
    public void init() {
        int ods = encryptionConfig.getOd().size();
        CD_MAP = new ConcurrentHashMap<>(ods);
        OD_MAP = new ConcurrentHashMap<>(ods);
        if (StringUtils.hasText(encryptionConfig.getEv())) {
            int nonNegativeHash = encryptionConfig.getEv().hashCode() & 0x7FFFFFFF;
            char[] chars = Integer.toString(nonNegativeHash).toCharArray();
            for (char c : chars) {
                encryptionConfig.getCd().add(String.valueOf(c));
            }
            List<String> shuffle = this.shuffle(encryptionConfig.getCd(), new Random(nonNegativeHash));
            List<String> odShuffle = this.shuffle(encryptionConfig.getOd(), new Random(nonNegativeHash));
            int cds = encryptionConfig.getCd().size();
            Q = cds / ods;
            R = cds % ods;
            int start = 0;
            for (int i = 0; i < ods; i++) {
                String encryption = String.join("", shuffle.subList(start, Q + start));
                if (i == odShuffle.size()) {
                    encryption = String.join("", shuffle.subList(start, Q + R));
                }
                CD_MAP.put(odShuffle.get(i), encryption);
                OD_MAP.put(encryption, odShuffle.get(i));
                start = start + Q;
            }
        } else {
            encryptionConfig.getOd().stream().forEach(v -> {
                OD_MAP.put(v, v);
                OD_MAP.put(v, v);
            });
        }
    }
    private List<String> shuffle(Set<?> set, Random rnd) {
        List list = new ArrayList(set);
        int size = list.size();
        if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
            for (int i = size; i > 1; i--) {
                this.swap(list, i - 1, rnd.nextInt(i));
            }
        } else {
            Object arr[] = list.toArray();
            for (int i = size; i > 1; i--) {
                this.swap(arr, i - 1, rnd.nextInt(i));
            }
            ListIterator it = list.listIterator();
            for (int i = 0; i < arr.length; i++) {
                it.next();
                it.set(arr[i]);
            }
        }
        return list;

    }
    private void swap(List<?> list, int i, int j) {
        final List l = list;
        l.set(i, l.set(j, l.get(i)));
    }

    private void swap(Object[] arr, int i, int j) {
        Object tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
